const NewsService = require("../../service/admin/NewsService")

const NewsController = {
    create: async (req, res) => {
        const newsData = {
            ...req.body,
            cover: `/newsuploads/${req.file.filename}`
        }
        await NewsService.create(newsData)
        res.send({
            code: 200,
            msg: '创建成功'
        })
    },
    getList: async (req, res) => {
        const result = await NewsService.getList(req.params.id)
        res.send({
            code: 200,
            msg: '获取成功',
            data: result
        })
    },
    changeStatus: async (req, res) => {
        await NewsService.changeStatus(req.params.id)
        res.send({
            code: 200,
            msg: '状态修改成功'
        })
    },
    uploadImg: async (req, res) => {
        res.send({
            url: `http://127.0.0.1:3000/newsuploads/${req.file.filename}`
        })
    },
    delete: async (req, res) => {
        await NewsService.delete(req.params.id)
        res.send({
            code: 200,
            msg: '删除成功'
        })
    },
    edit: async (req, res) => {
        let newsData = {}
        if (req.file) {
            newsData = {
                ...req.body,
                cover: `/newsuploads/${req.file.filename}`
            }
        } else {
            newsData = {
                ...req.body,
            }
        }

        await NewsService.edit(newsData)
        res.send({
            code: 200,
            msg: '更新成功'
        })
    }
}

module.exports = NewsController